//
//  BSMenuController.m
//  BusinessSchool
//
//  Created by 化 on 14-6-18.
//  Copyright (c) 2014年 化. All rights reserved.
//

#import "BSMenuController.h"
#import "BSServerController.h"
#import "BSShoppController.h"
#import "MSNoticeViewController.h"
#import "BSMettController.h"
#import "MSSettingListViewController.h"

@interface BSMenuController ()
{
    int height;
    ZBarReaderViewController *rader;
    NSTimer *timer;
    UIImageView *line;
    BOOL upOrdown;
    int num;
}

@end

@implementation BSMenuController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 0, 30, 30);
    [button setImage:[UIImage imageNamed:@"返回.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
    
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];//allocate titleView
    titleView.backgroundColor=[UIColor clearColor];
    titleView.alpha = 1;

    UIImageView *kuang1 = [[UIImageView alloc] initWithFrame:CGRectMake(46.4, 5, 107.2, 30)];
    kuang1.image = [UIImage imageNamed:@"logo-白.png"];
    kuang1.backgroundColor = [UIColor clearColor];
    [titleView addSubview:kuang1];

    self.navigationItem.titleView = titleView;
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"f0f0f0"];
    if (isIOS7) {
        height = 74  ;
    }else{
        height = 0;
    }
    
    NSArray *imagename = [NSArray arrayWithObjects:@"通知公告.png",@"学堂会议.png",@"二维码签到.png",@"手机支付.png",@"云海服务.png",@"云海商店.png",@"设置.png", nil];
    NSArray *lablename = [NSArray arrayWithObjects:@"通知公告",@"学堂会议",@"二维码签到",@"手机支付",@"云海服务",@"云海商店",@"个人设置", nil];
    for (int i = 0; i < 7; i++) {
        UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
        but.frame = CGRectMake(30+(i%3)*(200/3+30), 20+(i/3)*(200/3+40)+height, (200/3), 200/3);
        but.backgroundColor = [UIColor clearColor];
        [but setImage:[UIImage imageNamed:[imagename objectAtIndex:i]] forState:UIControlStateNormal];
        but.tag = i;
        [but addTarget:self action:@selector(butpress:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:but];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30+(i%3)*(200/3+30), 20+(i/3)*(200/3+40)+height+200/3, (200/3), 20)];
        label.backgroundColor = [UIColor clearColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = [lablename objectAtIndex:i];
        label.font = [UIFont fontWithName:@"Helvetica" size:12];
        [self.view addSubview:label];
        [label release];
        
    }
 
    
    // Do any additional setup after loading the view.
}

#pragma mark 返回
-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)butpress:(UIButton*)sender
{
    NSInteger tag = sender.tag;
    if (tag == 0) {
        MSNoticeViewController *notice = [[MSNoticeViewController alloc]init];
        [self.navigationController pushViewController:notice animated:YES];
        [notice release];
    }else if (tag == 1) {
        BSMettController *notice = [[BSMettController alloc]init];
        [self.navigationController pushViewController:notice animated:YES];
        [notice release];
    }else if (tag == 2) {
        [self saomiao];
    }else if (tag == 3) {
       
    }else if (tag == 4) {
        BSServerController *server = [[BSServerController alloc]init];
        [self.navigationController pushViewController:server animated:YES];
        [server release];
    }else if (tag == 5) {
        BSShoppController *shopp = [[BSShoppController alloc]init];
        [self.navigationController pushViewController:shopp animated:YES];
        [shopp release];
    }else if (tag == 6) {
        MSSettingListViewController *shopp = [[MSSettingListViewController alloc]init];
        [self.navigationController pushViewController:shopp animated:YES];
        [shopp release];
    }
}

-(void)saomiao
{
    //获取屏幕大小
    //获取屏幕大小
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        ShowMessage(@"该设备不能进行二维码扫描");
        return;
    }
    
    rader = [ZBarReaderViewController new];
    rader.readerDelegate = self;
    rader.showsHelpOnFail =NO;
    rader.showsZBarControls = NO;
    rader.supportedOrientationsMask = ZBarOrientationMaskAll;
    ZBarImageScanner *scanner = rader.scanner;
    [scanner setSymbology: ZBAR_I25
                   config: ZBAR_CFG_ENABLE
                       to: 0];
    float addheight = 60;
    if (isIOS7) {
        addheight = 0;
    }
    
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0+addheight, self.view.frame.size.width, self.view.frame.size.height)];
    view.backgroundColor = [UIColor clearColor];
    rader.cameraOverlayView = view;

    
    UIImageView * image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pick_bg.png"]];
    image.tag = 2;
    image.frame = CGRectMake(20, (self.view.frame.size.height-280)/2-60, 280, 280);
    image.center =  CGPointMake(self.view.frame.size.width/2, (self.view.frame.size.height-140)/2);
    image.autoresizingMask = UIViewAutoresizingNone;
    [view addSubview:image];
    

    
    line = [[UIImageView alloc] initWithFrame:CGRectMake(30, 10, 220, 2)];
    line.image = [UIImage imageNamed:@"linesao.png"];
    [image addSubview:line];
    //定时器，设定时间过1.5秒，
    
    timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation1) userInfo:nil repeats:YES];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setTitle:@"取消" forState:UIControlStateNormal];
    [backButton setBackgroundColor:[UIColor clearColor]];
    backButton.frame = CGRectMake(20, self.view.frame.size.height-60, 60, 50);
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:backButton];
    
    
    [self presentViewController:rader animated:YES completion:nil];
}

-(void)animation1
{
    if (upOrdown == NO)
    {
        num ++;
        line.frame = CGRectMake(30, 10+2*num, 220, 2);
        if (line.center.y >= line.superview.frame.size.height )
        {
            upOrdown = YES;
        }
    }
    else {
        num --;
        line.frame = CGRectMake(30, 10+2*num, 220, 2);
        //if (num == 0)
        if(line.center.y <=0)
        {
            upOrdown = NO;
        }
    }
    
    
}


- (void)back
{
    [timer invalidate];
    [rader dismissViewControllerAnimated:YES completion:nil];
}


- (void) imagePickerController: (UIImagePickerController*) reader
 didFinishPickingMediaWithInfo: (NSDictionary*) info
{
    id<NSFastEnumeration> results =
    [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    for(symbol in results)
        break;
    
    NSLog(@"%@", symbol.data);
//    扫描返回结果
    [self qiandao:[NSString stringWithFormat:@"%@",symbol.data]];
    
    [reader dismissViewControllerAnimated:YES completion:nil];
}


- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) orient
{
    return(YES);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma  mark 签到
-(void)qiandao:(NSString*)data
{
    NSMutableDictionary *defaults = [[NSMutableDictionary alloc] init];
    [defaults setObject:data forKey:@"meetingId"];
    [defaults setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] forKey:@"userId"];
    
    [[RequestServer instance] doActionAsync:0 withAction:@"com.haoqee.businessSchool.client.action.MeetingAction$signIn" withParameters:defaults withDelegate:self];
}


-(void) requestFinished:(NSNumber*)code withMessage:(NSString*)message withData:(id) data
{
    if (code == 0) {
        ShowMessage(@"签到成功成功");
    } else if([code intValue] == 1){
        ShowMessage(message);
    }else{
        ShowMessage(@"签到失败，请重新签到");
    }
}



@end
